package com.atguigu.cloud.controller;

import cn.hutool.core.date.DateUtil;
import com.atguigu.cloud.apis.PayFeignApi;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.resp.ReturnCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @auther zzyy
 * @create 2023-11-04 16:00
 */
@RestController
public class OrderController{
//    public static final String PaymentSrv_URL = "http://localhost:8001";//先写死，硬编码
//    public static final String PaymentSrv_URL = "http://cloud-payment-service";//服务注册中心上的微服务名称

    @Resource
    private PayFeignApi payFeignApi;

    @PostMapping("/feign/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO)
    {
        System.out.println("第一步：模拟本地addOrder新增订单成功(省略sql操作)，第二步：再开启addPay支付微服务远程调用");
        ResultData resultData = payFeignApi.addPay(payDTO);
        return resultData;
    }

    @GetMapping("/feign/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id)
    {
        System.out.println("-------支付微服务远程调用，按照id查询订单支付流水信息");
        ResultData resultData = null;
        try{
            System.out.println("调用开始-----:"+ DateUtil.now());
            resultData = payFeignApi.getPayInfo(id);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("调用结束-----:"+DateUtil.now());
            ResultData.fail(ReturnCodeEnum.RC500.getCode(),e.getMessage());
        }
         resultData = payFeignApi.getPayInfo(id);
        return resultData;
    }

    /**
     * openfeign天然支持负载均衡演示
     *
     * @return
     */
    @GetMapping(value = "/feign/pay/mylb")
    public String mylb()
    {
        return payFeignApi.mylb();
    }

//    @Autowired
//    private RestTemplate restTemplate;

    /**
     * 一般情况下，通过浏览器的地址栏输入url，发送的只能是get请求
     * 我们底层调用的是post方法，模拟消费者发送get请求，客户端消费者
     * 参数可以不添加@RequestBody
     * @param payDTO
     * @return
     */
//    @GetMapping("/consumer/pay/add")
//    public ResultData addOrder(PayDTO payDTO){
//        return restTemplate.postForObject(PaymentSrv_URL + "/pay/add",payDTO,ResultData.class);
//    }
//    // 删除+修改操作作为家庭作业，O(∩_∩)O。。。。。。。
//    @GetMapping("/consumer/pay/get/{id}")
//    public ResultData getPayInfo(@PathVariable("id") Integer id){
//        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/"+id, ResultData.class, id);
//    }
//
//    //家庭作业删除
//    @GetMapping(value = "/consumer/pay/del/{id}")
//    public ResultData delPayInfo(@PathVariable("id") Integer id){
//        ResponseEntity<ResultData> resultDataResponseEntity =
//                restTemplate.exchange(PaymentSrv_URL + "/pay/del/" + id, HttpMethod.DELETE,
//                        RequestEntity.EMPTY,ResultData.class,id);
//        ResultData resultData = resultDataResponseEntity.getBody();
//        return resultData;
//    }
//
//    //家庭作业修改
//    @GetMapping (value = "/consumer/pay/update")
//    public ResultData updatePayInfo(PayDTO payDTO){
//        //需要的是一个HttpEntity<T>，便创建一个这样的对象
//        HttpEntity<PayDTO> httpEntity = new HttpEntity<>(payDTO);
//
//        ResponseEntity<ResultData> resultDataResponseEntity =
//                restTemplate.exchange(PaymentSrv_URL + "/pay/update",HttpMethod.PUT,
//                        httpEntity,ResultData.class);
//        ResultData resultData = resultDataResponseEntity.getBody();
//        return resultData;
//    }
//
//    @GetMapping(value = "/consumer/pay/get/info")
//    private String getInfoByConsul()
//    {
//        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/info", String.class);
//    }
//
//    @Resource
//    private DiscoveryClient discoveryClient;
//    @GetMapping("/consumer/discovery")
//    public String discovery()
//    {
//        List<String> services = discoveryClient.getServices();
//        for (String element : services) {
//            System.out.println(element);
//        }
//
//        System.out.println("===================================");
//
//        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
//        for (ServiceInstance element : instances) {
//            System.out.println(element.getServiceId()+"\t"+element.getHost()+"\t"+element.getPort()+"\t"+element.getUri());
//        }
//
//        return instances.get(0).getServiceId()+":"+instances.get(0).getPort();
//    }
}